/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web;

import com.imenu.ejb.ImenuStoreFacade;
import com.imenu.ejb.ImenuUserFacade;
import com.imenu.entity.ImenuStore;
import com.imenu.entity.ImenuUser;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "UserServlet", urlPatterns = {"/user"})
public class UserServlet extends BaseServlet<ImenuUser> {

    /**如果command不能match到任何方法,默认显示的页面*/
    @Override
    protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward(request, response, "/user/login.jsp");
    }

    void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("UserServlet.register() idParam = "+request.getParameter("id"));
        String idParam = request.getParameter("idParam");
        ImenuUser user = null;
        if (idParam != null && idParam.length() > 0) {
            user = userFacade.find(Integer.valueOf(idParam));
        } else {
            user = new ImenuUser();
        }
        request.setAttribute("user", user);
        forward(request, response, "/user/userInfo.jsp");
    }
    @EJB
    ImenuStoreFacade storeFacade;

    void save(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idParam = request.getParameter("id");
        try {
            String password = request.getParameter("password");
            String confirm = request.getParameter("confirm");
            String username = request.getParameter("username");
            if (username == null || username.length() < 5) {
                throw new Exception("用户名不能为空，且长度至少为5.");
            }
            if (password == null || password.length() < 1 || confirm == null || confirm.length() < 1) {
                throw new Exception("密码不能为空。");
            }
            if (!password.equals(confirm)) {
                throw new Exception("两次密码输入不一致.");
            }
            if (idParam == null || idParam.length() == 0) {
                ImenuUser user = new ImenuUser();
                user.setUsername(username);
                user.setPassword(password);
                userFacade.create(user);
                request.getSession(true).setAttribute("userInSession", user);
                this.forward(request, response, "/store?command=storeInfo");
            } else {
                ImenuUser user = userFacade.find(Integer.valueOf(idParam));
                String storeId = request.getParameter("storeId");
                if (storeId != null && storeId.length() > 0) {
                    ImenuStore store = storeFacade.find(Integer.valueOf(storeId));
                    user.setStore(store);
                }
                this.parseRequest(request, user);
                userFacade.edit(user);
                request.getSession(true).setAttribute("userInSession", user);
                this.forward(request, response, "/menu?command=menuList");
            }
        } catch (Exception e) {
            error(request, response, e.getMessage());
        }
    }
    @EJB
    ImenuUserFacade userFacade;

    void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("UserServlet.login() username = " + username + " password=" + password);
        ImenuUser user = userFacade.findByUserName(username);
        if (user.getPassword().equals(password)) {
            request.getSession(true).setAttribute("userInSession", user);
            request.setAttribute("store", user.getStore());
            System.out.println("UserServlet.login() store = " + user.getStore());
            forward(request, response, "/store/storeInfo.jsp");
        } else {
            request.setAttribute("errMsg", "输入信息有误,无法登录.");
            forward(request, response, "/user/login.jsp");
        }
    }
}
